<?php
namespace app\api\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use app\api\model\UserModel as User;
use app\api\model\SmsModel;

class Register extends Base
{
    public $user;
    public function __construct()
    {
        parent::__construct();
        $this->user = new User();
      	$this->sms = new SmsModel();
    }
    public function register(){
        if($this->request->isPost()){
            $username = input("username",'');
            $phone = input("phone",'');
            $sms_code = input("sms_code",'');
            $password = input('password','');
            $password1 = input("password1",'');
            $yqm = input("yqm",'');
            $user_info = $this->user->getInfo(array("u_phone"=>$phone));
            $user_infos = $this->user->getInfo(array("u_nickname"=>$username));
            $yqm_info = $this->user->getInfo(array("yqm"=>$yqm));
            if($user_info || $user_infos){
                $this->error("用户已存在",'','-1');
            }
            if($password != $password1){
                $this->error("两次密码输入不一致",'','-1');
            }
            if(empty($yqm_info)){
                $this->error("推荐人不存在",'','-1');
            }
            /*$code = $this->sms->where(array("mobile"=>$phone,"times"=>0))->order("createtime desc")->find();
            if(empty($code)){
                $this->error("验证码已过期",'','-1');
            }else{
                if($code['code'] != $sms_code){
                    $this->error("验证码不正确",'','-1');
                }
            }*/
            $this->user->startTrans();
            $data = array();
            $data ['u_nickname'] = $username;
            $data ['u_pwd'] = md5(md5($password));
            $data ['u_phone'] = $phone;
            $data ['pid'] = $yqm_info['uid'];
            $data ['u_path'] = $yqm_info['u_path'].$yqm_info['uid'].",";
            $new_yqm = $this->makePassword(6);
            $new_user = $this->user->getInfo(array("yqm"=>$new_yqm));
            while ($new_user){
                $new_yqm = $this->makePassword(6);
                $new_user = $this->user->getInfo(array("yqm"=>$new_yqm));
            }
            $data ['yqm'] = $new_yqm;
            $data ['u_createtime'] = date("Y-m-d H:i:s");
            $res = $this->user->insert($data);
            if($res){
                $this->user->commit();
                $this->sms->where(array('mobile'=>$phone))->order("createtime desc")->update(array("times"=>1));
                $this->success("注册成功",'','1');
            }else{
                $this->user->rollback();
                $this->error("注册失败",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //生成邀请码
    public function makePassword($length = 16)
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        // 在 $chars 中随机取 $length 个数组元素键名
        $keys = array_rand($chars, $length);
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 将 $length 个数组元素连接成字符串
            $password .= $chars[$keys[$i]];
        }
        return $password;
    }
}
?>